package com.tzy.for10evalue.haoyong.excelexport.sysbackservice;

import com.aebiz.b2b2ccorecommon.config.kafka.SyncMessageService;
import com.aebiz.b2b2ccorecommon.utils.ApiParamDTO;
import com.aebiz.b2b2ccorecommon.utils.EmptyCheckUtil;
import com.aebiz.baseframework4ms.basewebapi.RetDTO;
import com.aebiz.baseframework4ms.utils.DateUtils;
import com.aebiz.baseframework4ms.utils.RetUtils;
import com.aebiz.plugins.b2b.common.dto.product.log.ExcelBatchOperateLog;
import com.aebiz.plugins.b2b.common.webapis.product.log.ExcelBathOperateApi;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * excel批量导入抽象方法
 * @description:excel批量导入抽象方法
 * @author: zhangtaiwei
 * @date: 2022/5/9 18:14
 **/
public abstract class AbstractImportService<T> implements ProductSkuImportService<T>{
    @Autowired
    protected SyncMessageService syncMessageService;
    @Autowired
    private ExcelBathOperateApi excelBathOperateApi;

    @Override
    public void productSkuImport(String excelName, List<T> importProductSkuExcels, ApiParamDTO apiParamDTO) {
        checkParam(importProductSkuExcels);
        checkNotNullParam(importProductSkuExcels);
        ExcelBatchOperateLog log = new ExcelBatchOperateLog();
        final String logName = apiParamDTO.getOperName() + DateUtils.getNowTimeStr(DateUtils.FORMAT_DATETIME_LINE_STR) + excelName;
        Integer type = getImportType();
        log.setExcelName(logName);
        log.setCount(importProductSkuExcels.size());
        log.setHandleNum(0);
        log.setOper(apiParamDTO.getOper());
        log.setType(type);
        log.setOrgId(apiParamDTO.getOrgId());
        log.setOperName(apiParamDTO.getOperName());
        RetDTO retDTO = excelBathOperateApi.insertLog(log);
        RetUtils.judgeVoidRetSuccess(retDTO);
        //发送消息
        sendWithoutTransactionMsg(importProductSkuExcels,logName,apiParamDTO);
    }

    protected abstract void checkNotNullParam(List<T> importProductSkuExcels);

    /**
     * 校验必填
     * @param importProductSkuExcels
     */
    protected void checkParam(List<T> importProductSkuExcels) {
        if(CollectionUtils.isEmpty(importProductSkuExcels)){
            return;
        }
        importProductSkuExcels.forEach(EmptyCheckUtil::check);
    }

    /**
     * 获取日志type(数据库中groupBy一下，找个没有的值)
     * @return type
     */
    protected abstract Integer getImportType();

    /**
     * 发送导入无事务消息（错误数据已经友好返回，无需事务）
     * @param importProductSkuExcels excel
     * @param logName 日志名
     * @param apiParamDTO 登录人信息
     */
    protected abstract void sendWithoutTransactionMsg(List<T> importProductSkuExcels, String logName, ApiParamDTO apiParamDTO);

    public boolean isLegalDate(int length, String sDate, String format) {
        if ((sDate == null) || (sDate.length() != length)) {
            return false;
        }
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }
}
